package rs.dalao.syc.doctor.prescription.controller;/*
 *   @author:Casin
 *   @date:2023/8/12
 *   @code:
 */

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import rs.dalao.syc.common.pojo.authentication.CurrentPrincipal;
import rs.dalao.syc.common.pojo.vo.PageData;
import rs.dalao.syc.common.response.JsonResult;
import rs.dalao.syc.doctor.prescription.pojo.param.PresAddNewParam;
import rs.dalao.syc.doctor.prescription.pojo.vo.PatientListVO;
import rs.dalao.syc.doctor.prescription.pojo.vo.PatientNameVO;
import rs.dalao.syc.doctor.prescription.service.IPrescriptionService;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/doc/prescription")
@Api(tags = "1. 开处方功能")
@Slf4j
public class PrescriptionController {
    @Autowired
    private IPrescriptionService service;

    @GetMapping("/getAll")
    @ApiOperation("获取所有认证用户")
    @PreAuthorize("hasAuthority('/doc/prescription/getAll')")
    @ApiOperationSupport(order = 100)
    public JsonResult getAll() {
        List<PatientNameVO> all = service.getAll();
        return JsonResult.ok(all);
    }

    @PostMapping("/addNew")
    @ApiOperation("开处方")
    @PreAuthorize("hasAuthority('/doc/prescription/addNew')")
    @ApiOperationSupport(order = 200)
    public JsonResult addNew(@Valid PresAddNewParam addNewParam, @ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal){
        service.PrescriptionAddNew(addNewParam,currentPrincipal);
        return JsonResult.ok();
    }

    @GetMapping("/patientList")
    @PreAuthorize("hasAuthority('/doc/prescription/patientList')")
    @ApiOperation("获取病患信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "2", dataType = "long")
    })
    public JsonResult getPatientList(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,Integer page){
        log.debug("开始处理【查询用户列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        Long doctorId = currentPrincipal.getId();
        PageData<PatientListVO> pageData = service.getPatientList(doctorId,pageNum);
        return JsonResult.ok(pageData);
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("在医生病患管理中删除用户列表")
    @PreAuthorize("hasAuthority('/doc/prescription/delete')")
    @ApiOperationSupport(order = 100)
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        service.update(id);
        return JsonResult.ok();
    }
}
